<?php

namespace Happy\CommandsService\Parsers;

use Happy\CommandsService\Constants\SessionsConstants;

class CommandResponseParser
{
    private array $sessionTypeResponseParserMapper;

    public function __construct()
    {
        $this->sessionTypeResponseParserMapper = [
            SessionsConstants::DEFAULT_SESSION => function ($response) {
                if ($response['error'] == '') {
                    $success = true;
                    $code = 0;
                    $response = $response['output'];
                } else {
                    $success = false;
                    $code = 1;
                    $response = $response['error'];
                }

                return new ResponseObject($success, $code, $response);
            },
        ];
    }

    public function parseResponse(string $sessionType, $response): ResponseObject
    {
        return $this->sessionTypeResponseParserMapper[$sessionType]($response);
    }
}
